<template>
  <div class="container">
    <div class="page-header">
      <strong>上传服务器</strong>
    </div>
    <div class="filter-container">
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleUpdate()"
      >添加
      </el-button>
      <el-button
        :disabled="itemsChecked.length === 0"
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="deleteData()"
      >批量删除
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="tableLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @selection-change="selectionChange"
    >
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column label="ID" prop="id" align="center" width="60"></el-table-column>
      <el-table-column label="服务器名称" prop="name" min-width="120"></el-table-column>
      <el-table-column label="IP地址" prop="ip" min-width="140"></el-table-column>
      <el-table-column label="是否网管" align="center" width="90">
        <template slot-scope="{row}">
          {{ row.manager?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column label="上传数据" align="center" width="90">
        <template slot-scope="{row}">
          {{ row.uploadData?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column label="数据上传周期" align="center" width="120">
        <template slot-scope="{row}">
          {{ row.uploadDataInterval }}分钟
        </template>
      </el-table-column>
      <el-table-column label="上传告警" align="center" width="90">
        <template slot-scope="{row}">
          {{ row.uploadAlarm?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column label="告警期数据上传周期" align="center" width="150">
        <template slot-scope="{row}">
          {{ row.intervalWAlarm }}分钟
        </template>
      </el-table-column>
      <el-table-column label="断点自动续传数据" align="center" width="150">
        <template slot-scope="{row}">
          {{ row.autoResumeData?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column label="断点自动续传告警" align="center" width="150">
        <template slot-scope="{row}">
          {{ row.autoResumeAlarm?'是':'否' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteData(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit-upload-service ref="editUploadService" @save="save"></edit-upload-service>
  </div>
</template>

<script>
  import { getAllNorthbound, deleteNorthbound } from "@/api/northbound";
  import { dataEleTypeOptions } from '@/utils/enums.js';
  import EditUploadService from "./EditUploadService";
  import _ from "lodash";

  export default {
    components: {
      'edit-upload-service': EditUploadService
    },
    data() {
      return {
        dataEleTypeOptions,
        isSubmitLoading: false,
        tableLoading: false,
        tableKey: 0,
        itemsChecked: [],
        tableData: []
      };
    },
    computed: {},
    watch: {},
    created() {
      this.getAllNorthbound();
    },
    methods: {
      handleSearch() {
        this.getAllNorthbound();
      },
      getAllNorthbound() {
        this.tableLoading = true;
        getAllNorthbound().then(result => {
          this.tableData = result;
          this.tableLoading = false;
        });
        this.$emit("save");
      },
      selectionChange(selection) {
        this.itemsChecked = selection;
      },
      save(isCreat) {
        this.getAllNorthbound();
      },
      deleteData(id) {
        let selectedIds = [];
        if (id) {
          selectedIds = [id];
        } else {
          selectedIds = this.itemsChecked.map(item => _.parseInt(item.id));
          if (selectedIds.length <= 0) {
            this.$message({ message: "至少选中一条记录", type: "warning" });
            return;
          }
        }

        this.$confirm("删除后不可恢复， 是否继续?", "确定执行删除操作吗？", {
          type: "warning"
        })
          .then(() => {
            deleteNorthbound(selectedIds).then(() => {
              this.getAllNorthbound();
              this.$emit("save");
              this.$message({ message: "删除成功", type: "success" });
            });
          })
          .catch(() => {
          });
      },
      transType(type) {
        let returnVal = '';
        switch (type) {
          case 1:
            returnVal = 'DCS-COM';
            break;
          case 2:
            returnVal = 'ModbusRTU-主站';
            break;
          case 3:
            returnVal = 'ModbusRTU-从站';
            break;
          case 4:
            returnVal = 'ModbusTCP-主站';
            break;
          case 5:
            returnVal = 'ModbusTCP-从站';
            break;
          case 6:
            returnVal = 'DCS-TCP';
            break;
          case 8:
            returnVal = 'Webservice';
            break;
          case 9:
            returnVal = 'KafkaConsumer';
            break;
          default:
            returnVal = type;
            break;
        }
        return returnVal;
      },
      handleUpdate(row) {
        this.$refs['editUploadService'].show(row);
      }
    }
  };
</script>

<style lang="scss" scoped>
  .page-header {
    padding: 0;
    position: relative;
    margin: 0 0 15px;
    overflow: hidden;
    border-bottom: 2px solid #e4e7ed;

    strong {
      height: 38px;
      box-sizing: border-box;
      line-height: 38px;
      display: inline-block;
      list-style: none;
      font-size: 14px;
      font-weight: 500;
      color: #303133;
      position: relative;
    }

  }

  .full-container {
    min-height: calc(100vh - 50px);
  }
</style>
